package com.xzz.arr;

import java.util.Arrays;

/**
 * @author: hhz
 * @create: 2022-02-24 13:23
 * 1706. 球会落何处
 **/
public class FindBall {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(findBall(new int[][]{{1, 1, 1, -1, -1}, {1, 1, 1, -1, -1}, {-1, -1, -1, 1, 1}, {1, 1, 1, 1, -1}, {-1, -1, -1, -1, -1}})));

    }

    static int m;
    static int n;
    static int[][] g;

    public static int[] findBall(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        g = grid;
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = findVal(i);
        }
        return res;
    }

    public static int findVal(int i) {
        int r = 0;
        int c = i;
        while (r < m) {
            int ne = c + g[r][c];
            if (ne < 0 || ne >= n) {
                return -1;
            }
            if (g[r][c] != g[r][ne]) {
                return -1;
            }
            r++;
            c = ne;
        }
        return c;
    }
}
